package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.smallgame.UserSmallGameStageData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserSmallGameStageDaoImpl extends TempDataObjectDao<UserSmallGameStageData> {

    public static String getUserSmallGameStageDataSql = "select * from `t_u_smallgame_stage` where `userId`=?";

    public Map<Integer, UserSmallGameStageData> getUserSmallGameStageData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserSmallGameStageData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserSmallGameStageDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserSmallGameStageData data = parse(rs);
                if (null != data) {
                    dataMap.put(data.getType(), data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserSmallGameStageDataSql = "update `t_u_smallgame_stage` set stageLevelPass=?,modifyTime=? where userId=? and `type`=?";

    @Override
    protected boolean updateData(UserSmallGameStageData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getStageLevelPass(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getModifyTime()));

        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        if (getDb().executeNoneQuery(updateUserSmallGameStageDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserSmallGameStageDataSql = "insert into `t_u_smallgame_stage`(userId,`type`,stageLevelPass,modifyTime) values (?,?,?,?)";

    @Override
    protected boolean addData(UserSmallGameStageData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getStageLevelPass(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getModifyTime()));
        if (getDb().executeNoneQuery(insertUserSmallGameStageDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserSmallGameStageData parseData(ResultSet rs) throws SQLException {
        UserSmallGameStageData data = new UserSmallGameStageData();
        data.setUserId(rs.getLong("userId"));
        data.setType(rs.getInt("type"));
        data.setStageLevelPass(StringUtils.stringToIntegerList(rs.getString("stageLevelPass"), ","));
        data.setModifyTime(rs.getInt("modifyTime"));
        return data;
    }
}
